package com.sziit.se.highconcurrency.product.application.controller;



import com.sziit.se.highconcurrency.common.annotation.IgnoreCheck;
import com.sziit.se.highconcurrency.common.result.Result;
import com.sziit.se.highconcurrency.common.result.ResultUtil;
import com.sziit.se.highconcurrency.product.application.entity.Product;
import com.sziit.se.highconcurrency.product.application.service.IProductService;
import com.sziit.se.highconcurrency.common.tool.PrintThreadInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dongchunkeng
 * @since 2022-11-08
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/product/product")
public class ProductController {

    private final IProductService productService;

    @GetMapping
    @IgnoreCheck
    public Result getProductInfo() throws InterruptedException {
        PrintThreadInfo.printThreadInfo();
        return ResultUtil.data(productService.getAllData());
    }

    @PostMapping
    @IgnoreCheck
    public Result addProductInfo(
            @NotBlank(message = "productId为空")String productId,
            @NotBlank(message = "productName为空")String productName,
            @NotBlank(message = "inventory为空")int inventory,
            @NotBlank(message = "price为空")float price
    ){
        return ResultUtil.auto(productService.insertProduct(productId,productName, inventory, price));
    }

    @GetMapping
    @IgnoreCheck
    @RequestMapping("/query-by-id")
    public Result getProductById(@RequestParam(value = "product_id") String productId) throws InterruptedException {
        return ResultUtil.data(productService.getOneData(productId));
    }

    @PutMapping
    @IgnoreCheck
    @RequestMapping("/update-by-id")
    public Result updateProductById(@RequestBody Product product) throws Exception {
        Boolean success = productService.updateOneDate(product);
        if (false == success) {
            log.info("update product failed, the productId is " + product.getProductId());
            throw new Exception("update product failed, the productId is " + product.getProductId());
        }
        return ResultUtil.data(productService.getOneData(product.getProductId()));
    }
}
